//
//  BarView.m
//  SimpleMapView
//
//  Created by Stefan Ivanov on 11/28/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "BarView.h"

@implementation BarView

@synthesize pointIcon, pointAmount, currentPoints;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        //load the background color from the image with the pattern
        self.backgroundColor = [[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"barbackground.png"]]autorelease];
        
        //load the icon representing the points from the image and position it
        pointIcon = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"point.png"]]autorelease];
        [pointIcon setCenter:CGPointMake(25.0, 25.0)];
        //add to the view
        [self addSubview:pointIcon];
        
        //observe own value of the point to update the amount when necessary
        [self addObserver:self forKeyPath:@"currentPoints" options:NSKeyValueObservingOptionNew context:NULL];
        
        //create the label for displaying the amount of points and set its size and position
        pointAmount = [[[UILabel alloc] initWithFrame:CGRectMake(60, 5, 260, 40)]autorelease];
        //set up the text font and color, the contents and the bacgroung of the label to be transparent
        pointAmount.textColor = [UIColor purpleColor];
        [pointAmount setFont:[UIFont fontWithName:@"Helvetica-Bold" size:30.0]];
        [pointAmount setText: [NSString stringWithFormat:@"%d", currentPoints]];
        [pointAmount setBackgroundColor:[UIColor clearColor]];
        //add to the view
        [self addSubview:pointAmount];
        
        //make all 50% transparent for more map visibility
        self.alpha = 0.5;
        }
    return self;
}

-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    [pointAmount setText: [NSString stringWithFormat:@"%d", currentPoints]];
}

-(void) dealloc
{
    [self removeObserver:self forKeyPath:@"currentPoints"];
    [pointIcon release];
    [pointAmount release];
    [super dealloc];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
